//
//  UIVIewExtension.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import Foundation

import Foundation
import UIKit
extension UIView {
    
    typealias AnimateBlock = (()->())
    typealias TapBlock = ((_ view: UIView) -> ())
    
    static let animateKey:UnsafeRawPointer = UnsafeRawPointer(UnsafeMutablePointer<UInt8>.allocate(capacity: 1))
    static let tapKey:UnsafeRawPointer = UnsafeRawPointer(UnsafeMutablePointer<UInt8>.allocate(capacity: 1))
    
    func autoresizing(index:Int) {
        var arm:UIView.AutoresizingMask = UIView.AutoresizingMask()
        
        switch index {
        case 0:
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleHeight)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
        case 1:
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
            
        case 2:
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
        case 3:
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleHeight)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
        case 4:
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleHeight)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
        case 5:
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            //            arm.formUnion(UIViewAutoresizing.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
            break
        case 6:
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            //            arm.formUnion(UIViewAutoresizing.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
            arm.formUnion(UIView.AutoresizingMask.flexibleHeight)
        case 7:
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            
        default:
            arm.formUnion(UIView.AutoresizingMask.flexibleRightMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleLeftMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleBottomMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleTopMargin)
            arm.formUnion(UIView.AutoresizingMask.flexibleHeight)
            arm.formUnion(UIView.AutoresizingMask.flexibleWidth)
        }
        
        self.autoresizingMask = arm
    }
    
    func setCornerRadius(radius:CGFloat) {
        self.clipsToBounds = true
        self.layer.cornerRadius = radius
    }
    
    func setBorder(width:CGFloat,color:UIColor) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.cgColor
    }
    
    func setShadow(color:UIColor,offset:CGSize,opacity:Float,radius:CGFloat) {
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = offset
        self.layer.shadowOpacity = opacity;
        self.layer.shadowRadius = radius;
    }

    
    func drawDottedLine(strokeColor:UIColor,startPoint:CGPoint,endPoint:CGPoint)  {
        
        let dotteShapLayer = CAShapeLayer()
        let mdotteShapePath = CGMutablePath()
        dotteShapLayer.fillColor = UIColor.clear.cgColor
        dotteShapLayer.strokeColor = strokeColor.cgColor
        dotteShapLayer.lineWidth = 0.5
        mdotteShapePath.move(to: startPoint)
        mdotteShapePath.addLine(to: endPoint)
        dotteShapLayer.path = mdotteShapePath
        let arr :NSArray = NSArray(array: [10,5])
        dotteShapLayer.lineDashPhase = 0.5
        dotteShapLayer.lineDashPattern = arr as? [NSNumber]
        self.layer.addSublayer(dotteShapLayer)
    }
    
    func shakeAnimate(_ back: AnimateBlock)  {
        let animation:CAKeyframeAnimation = CAKeyframeAnimation.init(keyPath: "transform")
        animation.duration = 0.2
        var values:[NSValue] = []
        values.append(NSValue.init(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)))
        values.append(NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
        animation.values = values
        self.layer .add(animation, forKey: nil)
        
//        objc_setAssociatedObject(self, UIView.animateKey, back, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        self.perform(#selector(animateBack), with: nil, afterDelay: 0.2)
    }
    
    func shakeAnimateLevelLow(_ back: AnimateBlock)  {
        let animation:CAKeyframeAnimation = CAKeyframeAnimation.init(keyPath: "transform")
        animation.duration = 0.2
        var values:[NSValue] = []
        values.append(NSValue.init(caTransform3D: CATransform3DMakeScale(0.98, 0.98, 1.0)))
        values.append(NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
        animation.values = values
        self.layer .add(animation, forKey: nil)
        
//        objc_setAssociatedObject(self, UIView.animateKey, back, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        self.perform(#selector(animateBack), with: nil, afterDelay: 0.2)
    }
    
    
    @objc func animateBack()  {
        let back:AnimateBlock = objc_getAssociatedObject(self, UIView.animateKey) as! AnimateBlock
        back()
    }
    
    //  MARK: ***** tapAction
    func tapAction(block:@escaping TapBlock)  {
        
        objc_setAssociatedObject(self, UIView.tapKey, block, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        
        self.isUserInteractionEnabled = true
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapUpinside))
        self.addGestureRecognizer(tap)
    }
    
    @objc func tapUpinside()  {
        let back:TapBlock = objc_getAssociatedObject(self, UIView.tapKey) as! TapBlock
        back(self)
    }
    
}
